package com.example.hometown_walker.exception;

import com.netflix.hystrix.exception.HystrixBadRequestException;
import lombok.Getter;
import lombok.Setter;

import java.text.MessageFormat;

/**
 * @description: 自定义校验异常，向下传递，不熔断
 * <p>
 * HystrixBadRequestException的特殊对待逻辑：它发出的事件类型是HystrixEventType.BAD_REQUEST，
 * 而此事件类型是不会被HealthCounts作为健康指标所统计的，因此该异常不会触发熔断器
 * <p>
 * <p>
 * 断路器Hystrix全面解析:https://baijiahao.baidu.com/s?id=1623004854011062838&wfr=spider&for=pc
 * <p>
 * @author: chenchaoran
 * @date: 2020-04-02
 */
@Getter
@Setter
public class ServiceException extends HystrixBadRequestException {
    private static final long serialVersionUID = -1151266074873468175L;

    private int code;
    private String msg;

    public ServiceException(String message) {
        super(message);
        this.msg = message;
    }

    public ServiceException(String message, Throwable cause) {
        super(message, cause);
        this.msg = message;
    }

    public ServiceException(int code, String msg) {
        super(msg);
        this.code = code;
        this.msg = msg;
    }

    public ServiceException(ErrorCode errorCode, Object...args) {
        super(MessageFormat.format(errorCode.getMsg(), args));
        this.code = errorCode.getCode();
        this.msg = super.getMessage();
    }

}
